/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.phoneBook.client;

import java.util.ArrayList;

import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;


public class Contact {
	private DebugDialogBox errorNotifier  = DebugDialogBox.getErrorNotifier();
	private JSONObject ContactInfo = null;
	
	Contact(JSONObject ContactInfo)
	{
		this.ContactInfo = ContactInfo;
		
	}
	
	public String getName(){
		
		String name = "";
		
		try {
			name = GoogleContacts.fixNameFormat(ContactInfo.get("title").isObject().get("$t").isString().stringValue());
		} catch (Exception e) {
			if (PhoneBook.debug)
				errorNotifier.showError(1, "No contact title");
			
		}
		
		return name;
	}

	public ArrayList <PhoneNumber> getPhoneNumbers(){
		
		
		
		ArrayList<PhoneNumber> numbers = new ArrayList<PhoneNumber>();
		JSONArray phoneList = null;
		try {
			
			if(ContactInfo.get("gd$phoneNumber")!=null)
				phoneList = ContactInfo.get("gd$phoneNumber").isArray();
			
			if(phoneList== null)
				return numbers;
			
			
			for (int i = 0; i<phoneList.size();i++)
			{
				numbers.add(new PhoneNumber(phoneList.get(i).isObject()));
			}
		} 
		catch (Exception e) {
		
			if (PhoneBook.debug)
				errorNotifier.showError(1, "error in Contact get Numbers"+e.getMessage());
		}
		
		return numbers;
		
	}
	
}
